const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const buf2hex = buffer => {
  return Array.prototype.map.call(new Uint8Array(buffer),
    x => ('00' + x.toString(16)).slice(-2)).join('')
}

const hex2buf = hex => {
  const bytes = []
  for (let i = 0; i < hex.length; i += 2) bytes.push(parseInt(hex.substr(i, 2), 16))
  return new Uint8Array(bytes)
}


const log = msg => {
  console.log('[NFC]', msg)
  wx.showToast({ title: msg, icon: 'none', duration: 2000 })
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}



module.exports = {
  formatTime,buf2hex,hex2buf,log 
}
